package me.pplay.playtalk.util;

import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.drawable.Drawable;
import android.util.Base64;

public class SkyUtil {
	public static String Base64encode(String str)
	{
		String[] string ;
		String tmp = Base64.encodeToString(str.getBytes(),Base64.DEFAULT);
		string = tmp.split("\n");
		tmp = string[0];
		return tmp;
	}
	public static Drawable setGrayDrawble(Drawable mDrawable)
	{
		mDrawable.mutate();  
		ColorMatrix cm = new ColorMatrix();  
		cm.setSaturation(0);  
		ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);  
		mDrawable.setColorFilter(cf); 
		return mDrawable;
	}
	public static String Base64decode(String str)
	{
		String tmp = new String(Base64.decode(str,Base64.DEFAULT));
		return tmp;
	}
	
	public static String encryptByMD5(String str) throws Exception {
		String[] string ;
		MessageDigest md = MessageDigest.getInstance("MD5");
		byte[] bts = md.digest(str.getBytes());
		// 采用Base64算法，将加密后的字节变成字符串
		String pwd = Base64.encodeToString(bts, Base64.DEFAULT);
		string = pwd.split("\n");
		pwd = string[0];
		return pwd;
	}
	//=====================判断邮件email是否正确格式
    public static boolean checkEmail(String email){
        Pattern pattern = Pattern.compile("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
        Matcher matcher = pattern.matcher(email);
        if (matcher.matches()) {
            return true;
        }
        return false;
    }
    
    public static final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
            "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
            "\\@" +
            "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
            "(" +
            "\\." +
            "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
            ")+"
        );
//come from smspop
    public static boolean isEmailAddress(String email) {
        if (email == null) {
            return false;
        }
        return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
    } 
    
//=====================判断手机号phone是否正确格式
	public static boolean checkPhone(String mobiles) {
	//	Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
		//Pattern p = Pattern.compile("^\\+(?:[0-9] ?){6,14}[0-9]$");
		Pattern p = Pattern.compile("^[0-9]*$");
		Matcher m = p.matcher(mobiles);
		System.out.println(m.matches() + "---");
		return m.matches();
	}
}
